package com.cpecc.dao.wuzi;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.BaoFei;
import com.cpecc.model.wuzi.BaoFeiJL;

@Repository
public class BaoFeiJLDao {
	
	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void save(BaoFeiJL baoFeiJL){
		Session session=sessionFactory.getCurrentSession();
		session.save(baoFeiJL);
		session.flush();
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFeiJL> getAllJL(String baoFeiId){
		String hql="from BaoFeiJL t where t.baoFei.id=? order by t.caoZuoRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, baoFeiId);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<BaoFei> getMySPJL(User user){
		String hql="select t.baoFei from BaoFeiJL t where t.user.id=? group by t.baoFei order by t.baoFei.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getShenPiLZR(String baoFeiId){
		String hql="select t.user from BaoFeiJL t where t.baoFei.id=? and t.huanJie.id=707";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, baoFeiId);
		return query.list();
	}

	public BaoFeiJL last(BaoFei baoFei){
		String hql="from BaoFeiJL t where t.baoFei=? and t.huanJie.id=704 order by t.caoZuoRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, baoFei);
		return (BaoFeiJL) query.list().get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<User> listBuMen(BaoFei baoFei){
		String hql="select distinct(t.user) from BaoFeiJL t where t.baoFei=? and t.huanJie=702";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, baoFei);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<User> listHuiQian(BaoFei baoFei){
		String hql="select distinct(t.user) from BaoFeiJL t where t.baoFei=? and t.huanJie=707";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, baoFei);
		return query.list();
	}
}
